package com.wxxymaker.edu.controller;

import com.blade.ioc.annotation.Inject;
import com.blade.kit.StringKit;
import com.blade.mvc.annotation.Param;
import com.blade.mvc.annotation.Path;
import com.blade.mvc.annotation.PathParam;
import com.blade.mvc.annotation.PostRoute;
import com.blade.mvc.ui.RestResponse;
import com.wxxymaker.edu.model.entity.JoinTestPaper;
import com.wxxymaker.edu.model.entity.User;
import com.wxxymaker.edu.roles.CourseLearnRoles;
import com.wxxymaker.edu.roles.StudentRoles;
import com.wxxymaker.edu.roles.TeacherRoles;
import com.wxxymaker.edu.service.JoinTestPaperService;
import com.wxxymaker.edu.service.TestPaperService;
import com.wxxymaker.edu.utils.FileUtils;
import com.wxxymaker.edu.utils.TokenUtils;
import io.github.biezhi.anima.page.Page;

/**
 * @AUTHOR soft
 * @DATE 2018/10/12 19:44
 * @DESCRIBE
 */
@CourseLearnRoles
@Path(value = "/course/join_paper", restful = true)
public class JoinTestPaperHandler {

    @Inject
    private TestPaperService testPaperService;

    @Inject
    private JoinTestPaperService joinTestPaperService;

    @TeacherRoles
    @PostRoute("/t/page")
    public RestResponse tPage(@Param Integer kid,
                             @Param(defaultValue = "2") Integer status,
                             @Param(defaultValue = "1") Integer page,
                             @Param(defaultValue = "10") Integer size) {
        User user = TokenUtils.currentUser();
        Page<JoinTestPaper> paperPage = joinTestPaperService.page(kid, null, user.getId(), status,
                false, true, page, size);
        if (paperPage != null) {
            return RestResponse.ok(paperPage);
        }
        return RestResponse.fail();
    }

    @TeacherRoles
    @PostRoute("/:jpid/mark")
    public RestResponse mark(@PathParam Integer jpid,
                             @Param String score) {
        JoinTestPaper joinTestPaper = new JoinTestPaper();
        joinTestPaper.setId(jpid);
        joinTestPaper.setScore(Float.valueOf(score));
        joinTestPaper.setStatus(3);
        if (joinTestPaperService.change(joinTestPaper)) {
            return RestResponse.ok();
        }
        return RestResponse.fail();
    }

    /**
     * 用户获取考试记录
     * @param kid 课程id null获取所有
     */
    @StudentRoles
    @PostRoute("/u/page")
    public RestResponse page(@Param Integer kid,
                             @Param(defaultValue = "1") Integer page,
                             @Param(defaultValue = "10") Integer size) {
        User user = TokenUtils.currentUser();
        Page<JoinTestPaper> page1 = joinTestPaperService.page(kid, user.getId(),
                null, null, false, false, page, size);
        if (page1 != null) {
            return RestResponse.ok(page1);
        }
        return RestResponse.fail();
    }

    /**
     * 提交答案
     * @param kid 课程id
     * @param jpid 加入考试id
     * @param result 答案路径
     */
    @StudentRoles
    @PostRoute("/:kid/submit/:jpid")
    public RestResponse submit(@PathParam Integer kid,
                               @PathParam Integer jpid,
                               @Param String folder,
                               @Param String result) {
        String format = "/file/download?t=%s&k="+kid+"&d=%s&f=%s";
        if (jpid != null && StringKit.isNotEmpty(result)) {
            boolean first = true;
            JoinTestPaper joinTestPaper = joinTestPaperService.byId(jpid);
            joinTestPaper.setStatus(2);
            if (!StringKit.isBlank(joinTestPaper.getResult())) first = false;
            joinTestPaper.setResult(String.format(format, 3, folder, FileUtils.getFilename(result)));
            if (joinTestPaperService.change(joinTestPaper)) {
                if (first) testPaperService.addSubmits(joinTestPaper.getTestPaperId());
                return RestResponse.ok(joinTestPaper);
            }
        }
        return RestResponse.fail();
    }

    /**
     *  加入考试
     * @param kid 课程id
     * @param pid 试卷id
     * @param tea 老师id 发布者
     */
    @StudentRoles
    @PostRoute("/:kid/join/:pid")
    public RestResponse join(@PathParam Integer kid,
                             @PathParam Integer pid,
                             @Param Integer tea) {
        User user = TokenUtils.currentUser();
        if (user.getId().equals(tea)) {
            return RestResponse.fail("不能参加自己的考试！");
        }
        if (joinTestPaperService.isJoin(pid, user.getId())) {
            return RestResponse.fail("不用重复加入！");
        }
        JoinTestPaper joinTestPaper = new JoinTestPaper();
        joinTestPaper.setTeaId(tea);
        joinTestPaper.setUserId(user.getId());
        joinTestPaper.setTestPaperId(pid);
        joinTestPaper.setCourseId(kid);

        JoinTestPaper add = joinTestPaperService.add(joinTestPaper);
        if (add != null) {
            testPaperService.addDowns(pid);
            return RestResponse.ok(add);
        }
        return RestResponse.fail("加入考试失败！");
    }
}
